// C source file with the function definitions to handle BDL's MDL3 section

// check_j3dgraph_bdl_mdl3() function
// function to check if the MDL3 section of a BDL is valid
j3dgraph_bdl_mdl3_err_cd check_j3dgraph_bdl_mdl3(j3dgraph * file)
{
  // check params
  if (file == NULL)
    goto err;

  // check the MDL3 character array at section start 
  if (comp_bytes(((j3dgraph_gen_sec *) (file -> SEC)[J3DGRAPH_BDL_MDL3_POS]) -> START,
                 J3DGRAPH_BDL_SEC_START[J3DGRAPH_BDL_MDL3_POS][file -> ENDIAN - 1], 4) == 0)
    goto err;
  
  // TODO: add more checks
  
  // all good
  return J3DGRAPH_BDL_MDL3_ERR_NO_ERR;
  
  // invalid section
  err:
  return J3DGRAPH_BDL_MDL3_ERR_HEADER;
}

// append_j3dgraph_bdl_mdl3() function
// append a j3dgraph_bdl_mdl3 struct to an incomplete j3dgraph struct
j3dgraph_bdl_mdl3 * append_j3dgraph_bdl_mdl3(j3dgraph * file)
{
  // check if the section is valid
  j3dgraph_bdl_mdl3 * mdl3 = NULL;
  j3dgraph_bdl_mdl3_err_cd err_cd = check_j3dgraph_bdl_mdl3(file);
  if (err_cd < J3DGRAPH_BDL_MDL3_ERR_NO_ERR)
    goto err;
  
  // otherwise append the structure to j3dgraph
  mdl3 = allocate_memory(1, sizeof(j3dgraph_bdl_mdl3));
  if (mdl3 == NULL)
    goto err;
  
  // fill the structure
  // TODO: research the MDL3 structure
  
  // temp variables
  umax tmp = 0;
  j3dgraph_gen_sec * sec = (file -> SEC)[J3DGRAPH_BDL_MDL3_POS];
  byte * ptr = (sec -> START) + 8;
  
  // my additions
  mdl3 -> PATH = file -> PATH;
  mdl3 -> ENDIAN = file -> ENDIAN;
  mdl3 -> ERR_CD = err_cd;
  // actual structure
  mdl3 -> START = sec -> START;
  mdl3 -> SIZE = sec -> SIZE;  
  
  // remove the j3dgraph_gen_sec and append the new one
  free_memory((file -> SEC)[J3DGRAPH_BDL_MDL3_POS]);
  return ((file -> SEC)[J3DGRAPH_BDL_MDL3_POS] = mdl3);
  // failure
  err:
  free_memory(mdl3);
  return NULL;
}

// remove_j3dgraph_bdl_mdl3() function
// function to remove a j3dgraph_bdl_mdl3 structure from a j3dgraph structure
void remove_j3dgraph_bdl_mdl3(j3dgraph * file)
{
  // check params
  if (file == NULL || (file -> SEC) == NULL)
    return;  
  // free the memory allocated by append_j3dgraph_bdl_mdl3()
  free_memory((file -> SEC)[J3DGRAPH_BDL_MDL3_POS]);
  return;
}

// print_j3dgraph_bdl_mdl3() function
// print the information of the MDL3 section of a BDL
void print_j3dgraph_bdl_mdl3(j3dgraph_bdl_mdl3 * mdl3)
{
  // check ptr
  if (mdl3 == NULL || (mdl3 -> ERR_CD) < J3DGRAPH_BDL_MDL3_ERR_NO_ERR)
    return;
  
  // starting visual queue
  printf("\n### J3DGraph MDL3 section info start ###\n\n");
  
  // file info
  printf("File: %s\n", mdl3 -> PATH);
  printf("Section endian: %s\n", (mdl3 -> ENDIAN) == BIG ? "BIG" : "LITTLE");
  printf("Section error code: %u\n", mdl3 -> ERR_CD);
  
  // section header
  printf("\nSection start: ");
  print_ascii_arr(mdl3 -> START, 4);
  printf("Section size (bytes): %llu\n", get_std_uint(32, mdl3 -> SIZE, mdl3 -> ENDIAN));
  
  // TODO: research the MDL3 structure
  printf("...\n");
  
  // end of structure
  printf("\n### J3DGraph MDL3 section info end ###\n");
  return;
}
